package com.xieyong.leetcode;

public class Leetcode3443 {
    public static void main(String[] args) {
        Leetcode3443 leetcode = new Leetcode3443();
        String s = "NWSE";
        int k = 1;
        int i = leetcode.maxDistance(s, k);
        System.out.println(i);

    }


    public int maxDistance(String s, int k) {
        int res = 0;
        int nn = 0, ww = 0, ss = 0, ee = 0;
        for (char c : s.toCharArray()) {
            switch (c) {
                case 'N': nn++; break;
                case 'W': ww++; break;
                case 'S': ss++; break;
                case 'E': ee++; break;
            }
            int dist = Math.abs(nn - ss) + Math.abs(ww - ee) + 2 * Math.min(k, Math.min(nn,ss) + Math.min(ww,ee));
            res = Math.max(res, dist);
        }
        return res;
    }
}
